<template>
	<view class='warp' style="background-color: #FFFFFF;">
		<view class="form box-s1">
			<view><text class="red">*</text>设备分类：
				<picker style="display: inline-block;text-align: end;" @change="choosebank" :value="index1"
					:range="list" range-key="cate_name">
					<view class="uni-input">{{type}}</view>
				</picker>
			</view>
			<view class="flex">

				<text class="red">*</text>设备品牌： <combox ref="box1"
					style="background-color: #f4f5f6;width: 75%;display: block;" :value="keyword1" 
					placeholder="请选择您要添加的门店" emptyTips="暂无匹配门店" labelWidth="0px" :candidates="options1" @input="change">
				</combox>
			</view>
			<view><text class="red">*</text>设备名称：<input type="number" placeholder="请输入配件名称" v-model="store_name"
					:disabled="disabled"></view>
			<view><text class="red">*</text>设备数量：<uni-number-box style="display: inline-block;" :min="0"
					v-model="num"></uni-number-box>
			</view>
			<view><text class="red">*</text>核心配件：<input type="number" placeholder="请输入配件名称" v-model="store_info"
					:disabled="disabled"></view>
			<view>
				<view class="userId">
					<text class="red">*</text>设备图片： <text class="del red" v-if="url">*长按图片进行删除</text>
					<view>
						<view v-if="!url">
							<view class="iconfont img-sfz" @click="choose">&#xe61f;</view>

						</view>
						<image v-else :src="url" mode="" @longtap="del()"></image>
					</view>
					<view>
					</view>
				</view>
			</view>
		</view>

		<view class="btn" style="margin-bottom: 100rpx;">
			<button @click="confirm">确认添加</button>
			<button @click="back">取消添加</button>
		</view>
	</view>
</template>

<script>
	import {
		deviceAdd,
		getDeviceCategory
	} from '../../../api/api.js'
	import combox from "../../../components/uni-ui/lib/uni-combox/uni-combox.vue"

	export default {
		components: {
			combox
		},
		data() {
			return {
				type: "请选择设备分类",
				list: [],
				keyword1:"",
				index1: 0,
				user_id: "",
				num: 1,
				cate_id:"",
				cate_id1:"",
				store_name:"",
				store_info:"",
				url: "",
				newshop: [],
				options: [],
				options1: [],
				uploadUrl:""
			}

		},
		created() {
			this.datainit()
			this.uploadUrl = uni.getStorageSync('uploadUrl')
			let data = uni.getStorageSync('newshop')
			console.log(data)
			if (data ) {
				this.newshop = data
			}
		},
		methods: {
			// 返回
			back(){
				uni.navigateTo({
					url:"/pages/sf/yxsb/index"
				})
			},
			// 上传图片
			choose() {
				// 选择图片文件
				console.log('点击', this.disabled)
				if (this.disabled) return
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						that.url = res.tempFilePaths[0]
						uni.uploadFile({
							url: that.uploadUrl, //仅为示例，非真实的接口地址
							filePath: that.url,
							name: 'file',
							header: {
								"Authori-zation": 'Bearer ' + that.token
							},
							methods: 'post',
							success: (res) => {
								let data = JSON.parse(res.data)
								console.log('上传地址', data)
								if (data.status == 200) {
									that.url = data.data.url
									uni.showToast({
										title: data.msg,
										icon: "none"
									})
								}
							}
						});
					}
				});
			},
			// 删除
			del() {
				if (this.disabled) return
				uni.showModal({
					title: "提示",
					content: "确定删除该图片吗?",
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							this.url = ''
							return
						}
						uni.showToast({
							title: "操作取消!",
							icon: "none"
						})
					}

				})
			},
			// 获取模糊查询数据
			change() {
				let flag = false
				console.log(this.options, this.keyword1)
				
			},
			// 确认添加
			confirm() {
				console.log('点击添加')
					console.log('值',this.$refs.box1.inputVal)
					let flag = false
					this.options.forEach(item => {
						console.log('这里的1',item,this.$refs.box1.inputVal)
						if (item.cate_name == this.$refs.box1.inputVal) {
							this.cate_id1 = item.id
							flag = true
							console.log('这里的',this.cate_id1)
							return
						} 
						
						if (item.cate_name != this.$refs.box1.inputVal && flag == false){
							this.cate_id1 = this.$refs.box1.inputVal
						}
					})
				if (!this.url) return uni.showToast({
					title:"请上传图片!",
					icon:"none"
				})
			
				console.log('添加到购物车里面的',par,this.newshop)
				
				if(!this.cate_id || !this.cate_id1 ||  !this.store_name || !this.store_info) return uni.showToast({
					title:"请完善信息!",
					icon:"none"
				});
				let par ={
					cate:this.cate_id,
					cate1:this.cate_id1,
					store_name:this.store_name,
					store_info:this.store_info,
					num:this.num,
					image:this.url
				}
			
				if(!this.newshop){
					this.newshop = []
				}
				this.newshop.push(par)
				uni.setStorageSync('newshop',this.newshop)
				uni.showToast({
					title:'添加成功!',
					icon:'none'
				})
				uni.navigateTo({
					url:'/pages/sf/yxsb/index'
				})
			},
			datainit() {
				let data = {
					pid: 0
				}
				getDeviceCategory(data).then(res => {
					this.list = res.data
				})
			},

			// 选择设备分类
			choosebank(e) {
				this.type = this.list[e.detail.value].cate_name
				this.pid = this.list[e.detail.value].id
				this.cate_id = this.pid
				console.log('配件分类', e, this.type)
				let data = {
					pid: this.pid
				}
				let arr =[]
				getDeviceCategory(data).then(res => {
					this.options = res.data
					res.data.forEach(item => {
						arr.push(item.cate_name)
					})
					this.options1 = arr
					console.log('lieb',this.options1)
				})
			},
		}

	}
</script>

<style scoped lang='less'>
	.form {
		width: 80%;
		padding: 30rpx;
		padding-top: 0;
		border-radius: 20rpx;
		margin-top: 30rpx;
		margin-left: 50%;
		transform: translateX(-50%);

		input {
			display: inline-block;
			width: 65%;
			vertical-align: middle;
		}

		view {
			line-height: 80rpx;
			margin-top: 30rpx;
			/* border-bottom: 2rpx solid #f0efef; */
			color: #333;
		}

		>view:last-of-type {
			border: none;
		}
	}

	.btn-group {
		width: 100%;
		display: flex;
		height: 60rpx;
		justify-content: center;
		align-items: center;
		line-height: 60rpx;
		position: absolute;
		bottom: 0rpx;


	}

	.img-sfz {
		margin-top: 50rpx;
		text-align: center;
		color: #409EFF !important;
		font-size: 200rpx;
	}

	.del {
		color: #FD3833;
		font-size: 20rpx;
	}
</style>
